package com.wtgroup.demo.mianshi.算法.字符串.字符串匹配;

import com.wtgroup.demo.common.judge.Judger;
import com.wtgroup.demo.common.util.RandomUtil;
import com.wtgroup.demo.common.util.Sout;

/**
 * @author nisus
 * @version 0.1
 * @since 2023/5/20 9:46
 */
public class D_暴力 {
    public static void main(String[] args) {
        Judger.of(() -> {
            String str = RandomUtil.randomString(RandomUtil.BASE_KEY_BOARD, RandomUtil.randomInt(0, 40));
            String pattern = RandomUtil.randomString(RandomUtil.BASE_KEY_BOARD, RandomUtil.randomInt(0, 40));
            int res1 = KMP.kmp(str, pattern);
            int res2 = match(str, pattern);
            if (res1 != res2) {
                Sout.println(res1, res2, str, pattern);
            }
            return res1 == res2;
        }).setIterCount(10).start().print();
    }

    public static int match(String haystack, String needle) {
        if(needle.length() == 0 || haystack.length() == 0) {
            return -1;
        }

        int N1 = haystack.length();
        int N2 = needle.length();
        for (int i = 0; i <= N1-N2; i++) {
            boolean match = true;
            for (int j = 0; j < N2; j++) {
                if (haystack.charAt(i + j) != needle.charAt(j)) {
                    match = false;
                    break;
                }
            }
            if (match) {
                return i;
            }
        }

        return -1;
    }


}
